/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pessoa.niceshot;

import br.com.niceshot.Conexao;
import br.com.niceshot.PresetQueries;
import static java.lang.System.out;
import java.sql.PreparedStatement;
import java.util.LinkedHashMap;

/**
 *
 * @author Alexandre
 */
public class PessoaQueries extends PresetQueries {

    public String InsertPessoa(LinkedHashMap<String, String> tableData) {
        InsertData("pessoa", tableData);

        return selectMaxPessoaId();
    }

    public String selectMaxPessoaId() {
        int maxId = 0;
        try {
            con = Conexao.getConnection();
            stat = con.createStatement();

            rs = stat.executeQuery("SELECT MAX(id_pessoa) FROM pessoa");
            rs.next();

            maxId = Integer.parseInt(rs.getString(1));

        } catch (Exception e) {
            return ("Error");

        } finally {

            try {
                rs.close();
            } catch (Exception e) {
            }
            try {
                con.close();
            } catch (Exception e) {
            }
            try {
                stat.close();
            } catch (Exception e) {
            }
        }
        return String.valueOf(maxId);
    }

    public int idPessoaFk(String tabela, String campoId, String id) {

        try {
            con = Conexao.getConnection();
            stat = con.createStatement();
            rs = stat.executeQuery("SELECT " + campoId + ", id_pessoa FROM " + tabela);

            while (rs.next()) {
                if (rs.getString(1).equals(id)) {
                    return Integer.parseInt(rs.getString(2));
                }
            }

        } catch (Exception e) {
            out.println(e.getMessage());

        } finally {
            try {
                rs.close();
            } catch (Exception e) {
            }
            try {
                con.close();
            } catch (Exception e) {
            }
            try {
                stat.close();
            } catch (Exception e) {
            }
        }
        return 0;
    }

}
